home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / math / gle-3.000 / gle-3 / gle / inkey.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  13KB  |  416 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include "conio.h"
  6.  
  7. #ifndef EMXOS2                                  /* a.r. */
  8.         #include "bios.h"
  9. #else
  10.         #include <sys/types.h>
  11.         #include <conio.h>
  12.         #include <sys/kbdscan.h>
  13.         #include <sys/video.h>
  14. #endif
  15.  
  16. #include "edt.h"
  17. #define true (!false)
  18. #define false 0
  19. int fner(char *s);
  20. int scr_refresh(void);
  21. int scr_getch(void);
  22.  
  23. #ifdef MANIP
  24. int mjl_key(int c);
  25. #endif
  26.  
  27. #ifdef EMXOS2                                   /* a.r. */
  28. static int last_key = -1;
  29. static int os2getch (void)
  30. {
  31.   int c;
  32.  
  33.   if (last_key >= 0)
  34.         {
  35.           c = last_key;
  36.           last_key = -1;
  37.         }
  38.   else
  39.         c = _read_kbd (0, 1, 0);
  40.   return (c);
  41. }
  42. int kbhit()
  43. {
  44.         return _read_kbd(0,0,0);
  45. }
  46. delay(int i){;}
  47. #endif
  48.  
  49. #ifndef MANIP
  50. char *function_bar()
  51. {
  52.         return "F1-Help  F2-Save  F3-Load  F4-Saveas  F5-Errs  F9-Graph  F10-Drawit ^Z Quit";
  53. }
  54. #else
  55. char *function_bar()
  56. {
  57.         return "F1=Help  F2=Save  F3=Load    Pageup=Recall command     ^Z Quit";
  58. }
  59. #endif
  60.  
  61.  
  62. #ifndef SURFACE
  63. extern int in_recover,single_step;
  64. #endif
  65.  
  66. struct keymatch {int m; int val;};
  67. /* commands using hi 8 bits of bios, e.g. arrow keys */
  68. struct keymatch kmatch1[] = {
  69.         0x0e, edelete,      /* DOS ? */
  70.         0x4d, eright,
  71.         0x4b, eleft,
  72.         0x48, eup,
  73.         0x53, edelright,
  74.         0x50, edown,
  75.         0x4a, edelline,     /* DOS ? */
  76.         0x3b, ehelp,        /* F1   */
  77.         0x3c, esave,        /* F2   */
  78.         0x3d, eload,        /* F3   */
  79.         0x3e, esaveas,      /* F4   */
  80.         0x3f, eshowerror,   /* F5   */
  81.         0x43, egraphmenu,   /* F9   */
  82.         0x44, edrawit,      /* F10  */
  83.         0x49, epageup,
  84.         0x51, epagedown,
  85.         0x47, ebol,
  86.         0x4f, eeol,
  87. #ifdef EMXOS2                                   /* a.r. */
  88.  #ifndef MANIP
  89.         0x2d, eescape,      /* Alt-x */
  90.  #else
  91.         0x73, ebigleft,     /* ^left  */
  92.         0x74, ebigright,    /* ^right */
  93.         0x91, edelline,     /* ^up    */
  94.         0x8d, esearch,      /* ^down  */
  95.  #endif
  96. #endif
  97.         0,0
  98. };
  99. /* Normal key and ^ commands  commands */
  100. struct keymatch kmatch2[] = {
  101.         0x0d, ereturn,      /* RETURN */
  102.         0x03, equit,        /* ^c   */
  103.         0x04, eword,        /* ^d   */
  104.         0x05, eshowerror,   /* ^e   */
  105. #ifndef MANIP
  106.         0x06, efast,        /*      */
  107. #else
  108.         0x02, ebackline,    /* ^b   */
  109. #endif
  110. #ifdef EMXOS2                                           /* a.r. */
  111.         0x08, edelete,      /* Backspace, ^h */
  112. #else
  113.         0x08, ehelp,        /* ^h   */
  114. #endif
  115.         0x13, eshell,       /* ^s   */
  116.         0x14, etrace,       /* ^t   */
  117.         0x0c, efindnext,    /* ^l   */
  118.         0x15, eundelline,   /* ^u   */
  119.         0x19, edelline,     /* ^y   */
  120.         0x1a, eescape,      /* ^z   */
  121.         0x1b, eescape,      /* ESC  */
  122.         0,0
  123. };
  124. /* Control K commands */
  125. struct keymatch kmatch3[] = {
  126.         'b', eselect,
  127.         'v', emove,
  128.         'k', emark,
  129.         'c', ecopy,
  130.         'y', ecut,
  131.         'u', epaste,
  132.         'p', epaste,
  133.         'r', eblockread,
  134.         'w', eblockwrite,
  135.         'm', egraphmenu,
  136.         'l', eload,
  137.         'd', edrawit,
  138.         's', esave,
  139.         'x', equit,
  140.         0,0
  141. };
  142. /* Control Q commands */
  143. struct keymatch kmatch4[] = {
  144.         'f', esearch,
  145.         'c', eendoffile,
  146.         'r', etopoffile,
  147.         0,0
  148. };
  149.  
  150. #ifndef MANIP
  151. #ifndef EMXOS2                                  /* a.r. */
  152. text_inkey()
  153. {
  154.         int cc,i,c1,c2,ff;
  155.         scr_refresh();
  156. loop1:        cc = bioskey(0);
  157.         ff = bioskey(2);
  158.         c1 = (cc & 0xff00)>>8;
  159.         c2 = (cc & 0xff);
  160.         if (c1==45 && ((ff & 8)>0) ) return eexitnow;
  161.         for (i=0;kmatch1[i].m!=0;i++)
  162.                 if (kmatch1[i].m==c1) return kmatch1[i].val;
  163.  
  164.         switch(c2) {
  165.           default:
  166.             for (i=0;kmatch2[i].m!=0;i++)
  167.                 if (kmatch2[i].m==c2) return kmatch2[i].val;
  168.             break;
  169.           case 17:
  170.             fner("^Q  F=Find string,  R=Top of file,  C=End of file");
  171.             cc = bioskey(0);
  172.             c2 = (cc & 0xff);
  173.             if (c2<32) c2 = c2 + 'a' - 1;
  174.             c2 = tolower(c2);
  175.             for (i=0;kmatch4[i].m!=0;i++)
  176.                 if (kmatch4[i].m==c2) return kmatch4[i].val;
  177.             fner("Unrecognized Quick movement command");
  178.             goto loop1;
  179.           case 11:
  180.             fner("^K  B=begin block,  P=Paste,  Y=Cut,  K=End block");
  181.             cc = bioskey(0);
  182.             c2 = (cc & 0xff);
  183.             if (c2<32) c2 = c2 + 'a' - 1;
  184.             c2 = tolower(c2);
  185.             for (i=0;kmatch3[i].m!=0;i++)
  186.                 if (kmatch3[i].m==c2) return kmatch3[i].val;
  187.             fner("Unrecognized block command");
  188.             goto loop1;
  189.         }
  190.         return c2;
  191. }
  192.  
  193. #else   /* EMXOS2 */
  194.  
  195. text_inkey()
  196. {
  197.         int c,i,c2,c3;
  198.         scr_refresh();
  199. loop1:    
  200.           c = os2getch();
  201.           switch (c)
  202.                 {
  203.                 case 0:                   /* 8 bit keys: Alt-x, Fx, Home... */
  204.                   c2 = os2getch();
  205.                   for (i=0;kmatch1[i].m!=0;i++)
  206.                            if (kmatch1[i].m==c2) {return kmatch1[i].val;}
  207.                   break;
  208.  
  209.  
  210.                 case 0x11:                              /* Ctrl-q x sequences */
  211.                                 fner("^Q  F=Find string,  R=Top of file,  C=End of file");
  212.                                 c3 = os2getch();
  213.                                 c3 = (c3 & 0xff);
  214.                                 if (c3<32) c3 = c3 + 'a' - 1;
  215.                                 c3 = tolower(c3);
  216.                                 for (i=0;kmatch4[i].m!=0;i++)
  217.                                 if (kmatch4[i].m==c3) return kmatch4[i].val;
  218.                                 fner("Unrecognized Quick movement command");
  219.                                 goto loop1;
  220.  
  221.                 case 0x0b:                              /* Ctrl-k x sequences */
  222.                                 fner("^K  B=begin block,  P=Paste,  Y=Cut,  K=End block");
  223.                                 c3 = os2getch();
  224.                                 c3 = (c3 & 0xff);
  225.                                 if (c3<32) c3 = c3 + 'a' - 1;
  226.                                 c3 = tolower(c3);
  227.                                 for (i=0;kmatch3[i].m!=0;i++)
  228.                                 if (kmatch3[i].m==c3) return kmatch3[i].val;
  229.                                 fner("Unrecognized block command");
  230.                                 goto loop1;
  231.  
  232.                 default:                                /* normal and contol keys */
  233.                         for (i=0;kmatch2[i].m!=0;i++)
  234.                                 if (kmatch2[i].m==c) {return kmatch2[i].val;}
  235.                 }
  236. return c;            
  237. }
  238. #endif  /* EMXOS2 */
  239.  
  240. #else /* MANIP follows */
  241.  
  242. extern int iserr;
  243. FILE *jouptr;
  244. text_inkey()
  245. {
  246.         int c;
  247.         unsigned char ccc;
  248.         static int rdone;
  249.         if (in_recover && !rdone) {
  250. #ifdef unix
  251.                 if (jouptr==NULL) jouptr = fopen("manip_.j1","r");
  252. #else
  253.                 if (jouptr==NULL) jouptr = fopen("manip_.j1","rb");
  254. #endif
  255.                 if (jouptr==NULL) fner("Unable to open/read journal file manip_.j1");
  256.                 if (fread(&ccc,1,1,jouptr)!=1) {
  257.                         rdone = true;
  258.                         fclose(jouptr);
  259.                 }
  260.                 if (single_step) if (getch()!=' ') rdone = true;
  261.                 return ccc;
  262.         } else {
  263.                 c = xtext_inkey();
  264.                 mjl_key(c);
  265.                 return c;
  266.         }
  267. }
  268. #ifndef EMXOS2                  /* a.r. */
  269. xtext_inkey()
  270. {
  271.                 int cc,i,c1,c2,ff;
  272.                 scr_refresh();
  273. loop1:        cc = bioskey(0);
  274.                 ff = bioskey(2);
  275.                 c1 = (cc & 0xff00)>>8;
  276.                 c2 = (cc & 0xff);
  277. /*printf("%d %d ff=%d\n",c1,c2,ff); */
  278.                 if (c1==0x48 && ((ff & 2)>0) ) return esearch;
  279.                 if (c1==0x50 && ((ff & 2)>0) ) return edelline;
  280.                 if (c1==0x4b && ((ff & 2)>0) ) return ebigleft;
  281.                 if (c1==0x4d && ((ff & 2)>0) ) return ebigright;
  282.                 if (c1==45 && ((ff & 8)>0) ) return eexitnow;
  283.                 if (c2==0 || c2==8) {
  284.                                 for (i=0;kmatch1[i].m!=0;i++)
  285.                                 if (kmatch1[i].m==c1) {return kmatch1[i].val;}
  286.                 }
  287.                 switch(c2) {
  288.                   default:
  289.                         for (i=0;kmatch2[i].m!=0;i++)
  290.                                 if (kmatch2[i].m==c2) return kmatch2[i].val;
  291.                         break;
  292.                   case 17:
  293.                         fner("^Q  F=Find string,  R=Top of file,  C=End of file");
  294.                         cc = bioskey(0);
  295.                         c2 = (cc & 0xff);
  296.                         if (c2<32) c2 = c2 + 'a' - 1;
  297.                         c2 = tolower(c2);
  298.                         for (i=0;kmatch4[i].m!=0;i++)
  299.                                 if (kmatch4[i].m==c2) return kmatch4[i].val;
  300.                         fner("Unrecognized Quick movement command");
  301.                         goto loop1;
  302.                   case 11:
  303.                         fner("^K  B=begin block,  P=Paste,  Y=Cut,  K=End block");
  304.                         cc = bioskey(0);
  305.                         c2 = (cc & 0xff);
  306.                         if (c2<32) c2 = c2 + 'a' - 1;
  307.                         c2 = tolower(c2);
  308.                         for (i=0;kmatch3[i].m!=0;i++)
  309.                                 if (kmatch3[i].m==c2) return kmatch3[i].val;
  310.                         fner("Unrecognized block command");
  311.                         goto loop1;
  312.                 }
  313.                 return c2;
  314. }
  315.  
  316. #else   /* EMXOS2 --  a.r. */
  317.  
  318. xtext_inkey()
  319. {
  320.         int c,i,c1,c2,c3,ff, backspaces;
  321.         scr_refresh();
  322. loop1:    
  323.           c = os2getch();
  324.           switch (c)
  325.                 {
  326.                 case 0:                                 /* 8 bit keys: Alt-x, Fx, Home... */
  327.                   c2 = os2getch();
  328.                   for (i=0;kmatch1[i].m!=0;i++)
  329.                            if (kmatch1[i].m==c2) {return kmatch1[i].val;}
  330.                   break;
  331.  
  332.  
  333.                 case 0x11:                              /* Ctrl-q x sequences */
  334.                                 fner("^Q  F=Find string,  R=Top of file,  C=End of file");
  335.                                 c3 = os2getch();
  336.                                 c3 = (c3 & 0xff);
  337.                                 if (c3<32) c3 = c3 + 'a' - 1;
  338.                                 c3 = tolower(c3);
  339.                                 for (i=0;kmatch4[i].m!=0;i++)
  340.                                 if (kmatch4[i].m==c3) return kmatch4[i].val;
  341.                                 fner("Unrecognized Quick movement command");
  342.                                 goto loop1;
  343.  
  344.                 case 0x0b:                              /* Ctrl-k x sequences */
  345.                                 fner("^K  B=begin block,  P=Paste,  Y=Cut,  K=End block");
  346.                                 c3 = os2getch();
  347.                                 c3 = (c3 & 0xff);
  348.                                 if (c3<32) c3 = c3 + 'a' - 1;
  349.                                 c3 = tolower(c3);
  350.                                 for (i=0;kmatch3[i].m!=0;i++)
  351.                                 if (kmatch3[i].m==c3) return kmatch3[i].val;
  352.                                 fner("Unrecognized block command");
  353.                                 goto loop1;
  354.  
  355.                 default:                                /* normal and contol keys */
  356.                         for (i=0;kmatch2[i].m!=0;i++)
  357.                                 if (kmatch2[i].m==c) {return kmatch2[i].val;}
  358.                 }
  359. return c;            
  360. }
  361. #endif  /* EMXOS2 */
  362.  
  363. init_logging(char *infile)
  364. {
  365.         FILE *jptr;
  366.         unsigned char ccc;
  367.         static char buff[80];
  368.         if (in_recover) {
  369. #ifdef unix
  370.                 jouptr = fopen("manip_.j1","r");
  371. #else
  372.                 jouptr = fopen("manip_.j1","rb");
  373. #endif
  374.                 if (jouptr==NULL) fner("Unable to open/read journal file manip_.j1");
  375.                 fread(&ccc,1,1,jouptr);
  376.                 fread(buff,1,ccc,jouptr);
  377.                 if (strlen(infile)==0) strcpy(infile,buff);
  378.                 return;
  379.         }
  380.         unlink("manip_.j3");
  381.         rename("manip_.j2","manip_.j3");
  382.         rename("manip_.j1","manip_.j2");
  383. #ifdef unix
  384.         jptr = fopen("manip_.j1","w");
  385. #else
  386.         jptr = fopen("manip_.j1","wb");
  387. #endif
  388.         fputc(strlen(infile),jptr);
  389.         fwrite(infile,1,strlen(infile),jptr);
  390.         if (jptr==NULL) fner("Unable to open journal file manip_.j1");
  391.         fclose(jptr);
  392. }
  393. unsigned char mjl_buff[80];
  394. mjl_key(int c)
  395. {
  396.         int s;
  397.         if (in_recover) return;
  398.         if (c==0) return;
  399.         s = strlen(mjl_buff);
  400.         mjl_buff[s] = c;
  401.         mjl_buff[s+1] = 0;
  402.         if (s>40) mjl_flush();
  403. }
  404. mjl_flush()
  405. {
  406.         FILE *jptr;
  407.         if (in_recover) return;
  408.         if (strlen(mjl_buff)<7) return;
  409.         jptr = fopen("manip_.j1","ab");
  410.         if (jptr==NULL) fner("Unable to append to journal file manip_.j1");
  411.         fwrite(mjl_buff,1,strlen(mjl_buff),jptr);
  412.         fclose(jptr);
  413.         mjl_buff[0] = 0;
  414. }
  415.  
  416. #endif